from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, mixins
from rest_framework.permissions import IsAuthenticated

from areas.models import Areas
from areas.serilaizers import AreasListSerializer, SubAreaSerializer
from rest_framework_extensions.cache.mixins import CacheResponseMixin


class AreasViewset(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    list:
    城市区域信息

    retrive：
    返回省或者市的下属行政区
    """
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        if self.action == "list":
            return Areas.objects.filter(parent=None)
        else:
            return Areas.objects.all()

    def get_serializer_class(self):
        if self.action == "list":
            return AreasListSerializer
        return SubAreaSerializer
